const $api = require("../../api/index");

Page({
    data: {
        key: 'movie_showing',
        title: '影院热映',
        word: '',
        list: [],
        end: 0,
        loadState: 1,
        start: 0,
        count: 10,
        total: 0
    },
    async loadCollectionData() {
        var loadState = 1;
        this.setData({
            loadState: loadState
        });
        try {
            var result,data = {};
            if(this.data.key == "suggestion") {
                result = await $api.loadSuggestion(this.data.start, this.data.count);
                data.items = result.data.items;
                data.start = result.data.start;
                data.total = result.data.total;
            } else if(this.data.key == "search") {
                result = await $api.loadSearch(this.data.word);
                var items = result.data.subjects.items;
                // var items = result.data.contents.items;
                // data.start = result.data.contents.start;
                // data.total = result.data.contents.total;
                data.items = [];
                for (var index in items) {
                    var m = items[index];
                    if(m.target_type == "movie") {
                        var target = m.target;
                        data.items.push({
                            id: target.id,
                            cover: {
                                url: target.cover_url
                            },
                            title: target.title,
                            rating: target.rating
                        });
                    }
                }
            } else {
                result = await $api.loadSubjectCollection(this.data.key, this.data.start, this.data.count);
                data.items = result.data.subject_collection_items;
                data.start = result.data.start;
                data.total = result.data.total;
            }
            //console.log("loadCollectionData:", result);
            if(result.statusCode == 200){
                loadState = 2;
                //console.log(result.data.start, result.data.total);
                this.data.total = result.data.total;
                this.data.list.push(...data.items);
            } else {
                wx.showToast({
                    title: result.data.localized_message,
                    icon: "error",
                    duration: 2000
                });
            }
        } catch (error) {
            console.log('error:', error);
            loadState = 3;
            wx.showToast({
                title: '网络异常',
                icon: "error",
                duration: 2000
            });
        }
        this.data.start = this.data.start + this.data.count;
        // 刚好整除就不用补位
        var end = this.data.list.length%3;
        end = end == 0 ? end : 3 - end;
        this.setData({
            list: this.data.list,
            end: end,
            loadState: loadState,
            start: this.data.start,
            total: this.data.total
        });
    },
    onLoad(options) {
        //console.log("collection-onLoad:", options);
        this.data.key = options.key;
        this.data.title = options.title;
        this.data.word = options.word;
        wx.setNavigationBarTitle({
            title: this.data.title
        });
        this.loadCollectionData();
    },
    async onReachBottom() {
        //页面上拉触底事件的处理函数
        if(this.data.loadState != 1 && this.data.start < this.data.total) {
            // console.log('onReachBottom');
            await this.loadCollectionData();
            //console.log('loaded data');
        }
    }
})